package testinit;

import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.Before;
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.thread.QueuedThreadPool;
import org.springframework.web.context.ContextLoaderListener;

public class PortableWebServer {

	private Server server;
//	private FileUpdataQueryDaoImpl fileUpdataQueryDaoImpl;
//	
//	public void setFileUpdataQueryDaoImpl(
//			FileUpdataQueryDaoImpl fileUpdataQueryDaoImpl) {
//		this.fileUpdataQueryDaoImpl = fileUpdataQueryDaoImpl;
//	}
	
	@Before
	public void setUpBefore() throws Exception {
		server = new Server();  
		QueuedThreadPool threadPool = new QueuedThreadPool();  
        threadPool.setMaxThreads(5);  
        threadPool.setMinThreads(1);
        server.setThreadPool(threadPool);  
        Connector connector = new SelectChannelConnector();  
        connector.setPort(8080);  
        server.addConnector(connector);
        
        ContextLoaderListener listener = new ContextLoaderListener();
        
//        WebAppContext context = new WebAppContext("WebContent", "/WebContent");  
        Context context = new Context(server, "/engine", Context.SESSIONS);
        
        Map<String, String> initParamsMap = new HashMap<String, String>(2);
//        initParamsMap.put("log4jConfigLocation", "/WEB-INF/config/log4j.properties");
        initParamsMap.put("contextConfigLocation", "/WEB-INF/config/applicationContext.xml");
        
        context.addEventListener(listener);
        context.setInitParams(initParamsMap);
        
        
        server.addHandler(context);
        server.setStopAtShutdown(true);  
        server.setSendServerVersion(true);  
        
        server.start();  
		 }
	
	@After
	public void tearDownAfter() throws Exception {
		this.server.stop();
	}
	
//	@Test
//	public void getAll(){
//		System.out.println(fileUpdataQueryDaoImpl.getUserAll());
//	}
public static void main(String[] args) {
	try {
		PortableWebServer portableWebServer = new PortableWebServer();
		portableWebServer.setUpBefore();
//		portableWebServer.getAll();
		portableWebServer.tearDownAfter();
	} catch (Exception e) {
		e.printStackTrace();
	}
}
}
